package com.platform.web.controller.system;

import java.util.List;

import com.platform.system.service.IRecommenderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.platform.common.annotation.Log;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.enums.BusinessType;
import com.platform.system.domain.ProProduct;
import com.platform.system.service.IProProductService;
import com.platform.common.utils.poi.ExcelUtil;
import com.platform.common.core.page.TableDataInfo;

/**
 * 商品Controller
 *
 * @author platform
 * @date 2025-02-23
 */
@Tag(name = "商品", description = "")
@RestController
@RequestMapping("/system/product")
public class ProProductController extends BaseController {
    @Autowired
    private IProProductService proProductService;
    @Autowired
    private IRecommenderService recommenderService;

    /**
     * 查询热销商品列表
     */
    @Operation(summary = "查询热销商品列表")
    @GetMapping("/sales")
    public AjaxResult sales() {
        return success(proProductService.selectProProductSalesList());
    }


    /**
     * 查询商品列表
     */
    @Operation(summary = "查询商品列表")
    @GetMapping("/byCategory/{categoryId}")
    public TableDataInfo selectProProductListByCategory(@PathVariable("categoryId") Long categoryId) {
        startPage();
        ProProduct proProduct = new ProProduct();
        proProduct.setCategoryId(categoryId);
        List<ProProduct> list = proProductService.selectProProductListByCategory(proProduct);
        return getDataTable(list);
    }

    /**
     * 查询商品列表
     */
    @Operation(summary = "查询商品列表")
    @GetMapping("/listAll")
    public TableDataInfo listAll(ProProduct proProduct) {
        startPage();
        List<ProProduct> list = proProductService.selectProProductListAll(proProduct);
        return getDataTable(list);
    }

    /**
     * 查询商品列表
     */
    @Operation(summary = "查询商品列表")
    @PreAuthorize("@ss.hasPermi('system:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProProduct proProduct) {
        startPage();
        List<ProProduct> list = proProductService.selectProProductList(proProduct);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @Operation(summary = "导出商品列表")
    @PreAuthorize("@ss.hasPermi('system:product:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProProduct proProduct) {
        List<ProProduct> list = proProductService.selectProProductList(proProduct);
        ExcelUtil<ProProduct> util = new ExcelUtil<ProProduct>(ProProduct.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @Operation(summary = "获取商品详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(proProductService.selectProProductById(id));
    }

    /**
     * 新增商品
     */
    @Operation(summary = "新增商品")
    @PreAuthorize("@ss.hasPermi('system:product:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProProduct proProduct) {
        proProduct.setUserId(getUserId());
        proProduct.setCreateBy(getUsername());
        return toAjax(proProductService.insertProProduct(proProduct));
    }

    /**
     * 修改商品
     */
    @Operation(summary = "修改商品")
//    @PreAuthorize("@ss.hasPermi('system:product:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProProduct proProduct) {
        proProduct.setUpdateBy(getUsername());
        return toAjax(proProductService.updateProProduct(proProduct));
    }

    /**
     * 删除商品
     */
    @Operation(summary = "删除商品")
    @PreAuthorize("@ss.hasPermi('system:product:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(proProductService.deleteProProductByIds(ids));
    }

    /**
     * 查询推荐商品列表
     */
    @GetMapping("/recommend/{userId}")
    public AjaxResult getRecommendations(@PathVariable Long userId) {
        return success(recommenderService.recommendItems(userId, 8));
    }

    /**
     * 查询推荐商品列表
     */
    @Operation(summary = "查询推荐商品列表")
    @GetMapping("/recommend")
    public AjaxResult recommend() {
        return success(proProductService.selectProProductRecommendList());
    }

}
